﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using mBlog.Data.Entities;
using EmitMapper;
using mBlog.Core.Extensions;
using AutoMapper;

namespace mBlog.Test
{
    [TestClass]
    public class AutoMapperTest
    {
        [TestMethod]
        public void ShouleMapEntityToModel()
        {
            mBlog.Data.mBlogContext blogContext = new mBlog.Data.mBlogContext();

            AutoMapper.Mapper.CreateMap<Comment, mBlog.Core.Models.Comment>().ForMember(c => c.Post, opt => opt.Ignore());
            AutoMapper.Mapper.CreateMap<Category, mBlog.Core.Models.Category>().ForMember(c => c.Posts, opt => opt.Ignore());
            AutoMapper.Mapper.CreateMap<Tag, mBlog.Core.Models.Tag>().ForMember(t => t.Posts, opt => opt.Ignore());

            Post post = blogContext.Posts.OrderByDescending(po => po.PublishedTime).First();

            AutoMapper.Mapper.CreateMap<Post, mBlog.Core.Models.Post>();
                //.ForMember(p => p.Comments, opt => opt.MapFrom<ICollection<Comment>>(p => p.Comments))
                //.ForMember(p => p.Categories, opt => opt.MapFrom<ICollection<Category>>(p => p.Categories))
                //.ForMember(p => p.Tags, opt => opt.MapFrom<ICollection<Tag>>(p => p.Tags));

            mBlog.Core.Models.Post newPost = Mapper.Map<Post, mBlog.Core.Models.Post>(post);

            Assert.IsNotNull(newPost);
            Assert.IsNotNull(newPost.Categories);
            Assert.IsNotNull(newPost.Comments);
            Assert.IsNotNull(newPost.Tags);

            Assert.IsTrue(newPost.Tags.Count > 0 );

        }
    }
}
